package com.xidian.zhiancloud.controller.riskcontrol.risksystem;

import com.github.pagehelper.Page;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.enums.UserResponseEnums;
import com.xidian.zhiancloud.model.riskcontrol.TRiskPointTypeStatusList;
import com.xidian.zhiancloud.model.riskcontrol.entity.TPointTypeStaEntity;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.service.riskcontrol.risksystem.TRiskPointTypeStatusService;
import com.xidian.zhiancloud.util.api.ExcelUtil2;
import com.xidian.zhiancloud.util.tools.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风险管控-系统设置-风险点类型设置
 */
@RestController
@RequestMapping("/triskpointtype")
public class TRiskPointTypeStatusController extends BaseController {

    @Autowired
    TRiskPointTypeStatusService tRiskPointTypeStatusService;

    /**
     * 系统设置-系统设置-风险点类型设置列表
     * @param reqdata 多对象集合
     * @Param riskcontrolId 风险点类型ID
     *
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getTRiskPointTypeStatusList")
    public ResultDTO getTRiskPointTypeStatusList(BaseReqQuery reqdata,
           @RequestParam(value = "riskcontrolId",required = false) String riskcontrolId){
        Long companyId=this.getSessionUserCompanyId();
        Page<TRiskPointTypeStatusList> pagedata =
                tRiskPointTypeStatusService.getTRiskPointTypeStatusList
                        (reqdata,companyId,riskcontrolId);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("datalist", pagedata);
        data.put("PageNum", pagedata.getPageNum());
        data.put("PageSize", pagedata.getPageSize());
        data.put("PageTotal", pagedata.getPages());
        data.put("total", pagedata.getTotal());
        return ResultDTO.successOf(data, GeneralResponseEnums.SUCCESS);

    }

    /**
     * 系统设置-风险点类型设置添加
     * @param riskcontrolId 风险点类型ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/addTRiskPointTypeStatus")
    public ResultDTO addTRiskPointTypeStatus
        (@RequestParam("riskcontrolId") String riskcontrolId,
         TRiskPointTypeStatusList tRPointTypeSta){
        Long companyId=this.getSessionUserCompanyId();
        Long userId=this.getSessionUserid();
        tRPointTypeSta.setRiskcontrolId(riskcontrolId);
        tRPointTypeSta.setCompanyId(companyId);
        tRPointTypeSta.setPersonAdd(String.valueOf(userId));
        tRiskPointTypeStatusService.addTRiskPointTypeStatus(tRPointTypeSta);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 系统设置-风险点类型设置修改
     * @param id 风险点表ID
     * @param riskcontrolId 风险点类型ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/updateTRiskPointTypeStatus")
    public ResultDTO updateTRiskPointTypeStatus(@RequestParam( "id") String id,
     @RequestParam("riskcontrolId") String riskcontrolId,
     TRiskPointTypeStatusList tRPointTypeSta){
        Long companyId=this.getSessionUserCompanyId();
        Long userId=this.getSessionUserid();
        tRPointTypeSta.setRiskcontrolId(riskcontrolId);
        tRPointTypeSta.setCompanyId(companyId);
        tRPointTypeSta.setPersonAdd(String.valueOf(userId));
        tRiskPointTypeStatusService.updateTRiskPointTypeStatus
                (id,tRPointTypeSta);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 系统设置-风险点类型设置删除
     * @param id 风险点表ID
     *
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/deleteTRiskPointTypeStatus")
    public ResultDTO deleteTRiskPointTypeStatus(@RequestParam("id") String id) {
        tRiskPointTypeStatusService.deleteTRiskPointTypeStatus(id);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     *  -导出风险点类型设置
     * @param riskcontrolId 风险点类型ID
     */
    @GetMapping("/exportTPointTypeStaData")
    public void exportTPointTypeStaData (HttpServletResponse response,
           @RequestParam("riskcontrolId") String riskcontrolId)
            throws IOException {
        Long companyId=this.getSessionUserCompanyId();
        Page<TRiskPointTypeStatusList> pagedata = tRiskPointTypeStatusService
                .getExportTPointTypeStaData(companyId,riskcontrolId);
        List<TRiskPointTypeStatusList> list = pagedata.getResult();
        ExcelUtil2<TRiskPointTypeStatusList> util = new
                ExcelUtil2<TRiskPointTypeStatusList>(TRiskPointTypeStatusList.class);
        util.exportExcel(response, list, "风险点类型设置");
    }

    /**
     * 导入风险点类型设置
     * @param riskcontrolId 风险点类型ID
     *       ss00 设备设施
     *       zy00 作业活动
     *       gy00 工艺节点
     * @param file 文件
     * @return
     */
    @RequestMapping("/importTPointTypeStaData")
    @ResponseBody
    public ResultDTO importTPointTypeStaData
        (@RequestParam("riskcontrolId") String riskcontrolId,
         @RequestParam MultipartFile file){
        Long companyId=this.getSessionUserCompanyId();
        Long userId=this.getSessionUserid();
        try{
            Object obj = TPointTypeStaEntity.class.newInstance();
            List<Object> list = ExcelUtils.importBaseExcel0
                    (file,"风险点类型设置",obj);
            if(riskcontrolId.equals("ss00")){//设备设施
                for(int i=0;i<list.size();i++){
                    TPointTypeStaEntity poTypeSta =
                            (TPointTypeStaEntity)list.get(i);
                    tRiskPointTypeStatusService.addTPointTypeStaSS00Entity
                            (companyId,userId,poTypeSta);
                }
            }else if(riskcontrolId.equals("zy00")){//作业活动
                for(int i=0;i<list.size();i++){
                    TPointTypeStaEntity poTypeSta =
                            (TPointTypeStaEntity)list.get(i);
                    tRiskPointTypeStatusService.addTPointTypeStaZY00Entity
                            (companyId,userId,poTypeSta);
                }
            }else if(riskcontrolId.equals("gy00")){//工艺节点
                for(int i=0;i<list.size();i++){
                    TPointTypeStaEntity poTypeSta =
                            (TPointTypeStaEntity)list.get(i);
                    tRiskPointTypeStatusService.addTPointTypeStaGY00Entity
                            (companyId,userId,poTypeSta);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultDTO.errorOf(UserResponseEnums.DATA_IMPORT_FAILED_ERROR);
        }
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

}
